'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _autoComplete = require('antd/lib/auto-complete');

var _autoComplete2 = _interopRequireDefault(_autoComplete);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _utils = require('../../utils');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

// import Input from 'antd/lib/input';
var SelectInput = function (_React$Component) {
  (0, _inherits3['default'])(SelectInput, _React$Component);
  (0, _createClass3['default'])(SelectInput, null, [{
    key: 'getDerivedStateFromProps',
    value: function getDerivedStateFromProps(props, _ref) {
      var prevProps = _ref.prevProps;

      var nextState = {
        prevProps: props
      };
      if (prevProps && prevProps.value !== props.value) {
        nextState.value = props.value;
      }
      return nextState;
    }
  }]);

  function SelectInput(props) {
    (0, _classCallCheck3['default'])(this, SelectInput);

    var _this = (0, _possibleConstructorReturn3['default'])(this, (SelectInput.__proto__ || Object.getPrototypeOf(SelectInput)).call(this, props));

    _this.onChangeEnd = function (e) {
      var target = e.target;

      var v = target ? target.value : e;
      if (v !== _this.props.value) {
        _this.setState({
          showAll: false,
          value: v
        });
        _this.props.onChange(v);
      }
    };

    _this.onFocus = function () {
      _this.setState({
        showAll: true
      });
    };

    _this.onChange = function (value) {
      _this.isSelect = false;
      _this.setState({
        value: value,
        showAll: false
      });
    };

    _this.filterOption = function (input, option) {
      if (_this.state.showAll) {
        return true;
      }
      return option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0;
    };

    _this.state = {
      showAll: false,
      value: props.value
    };
    return _this;
  }

  (0, _createClass3['default'])(SelectInput, [{
    key: 'render',
    value: function render() {
      var _this2 = this;

      var value = this.state.value;
      var _props = this.props,
          children = _props.children,
          onChange = _props.onChange,
          props = (0, _objectWithoutProperties3['default'])(_props, ['children', 'onChange']);

      return _react2['default'].createElement(
        _autoComplete2['default'],
        (0, _extends3['default'])({
          className: 'select-input',
          style: { width: '100%' }
        }, props, {
          placeholder: this.props.placeholder || '--',
          filterOption: this.filterOption,
          onBlur: this.onChangeEnd,
          onFocus: this.onFocus,
          onChange: this.onChange,
          onSelect: function onSelect(v) {
            _this2.isSelect = true;
            _this2.onChangeEnd(v);
          },
          value: value,
          getPopupContainer: function getPopupContainer(node) {
            return (0, _utils.getParentNode)(node, 'editor-list');
          },
          size: 'small',
          dropdownMatchSelectWidth: false,
          dropdownClassName: 'editor-list-dropdown'
        }),
        children
      );
    }
  }]);
  return SelectInput;
}(_react2['default'].Component);

SelectInput.propTypes = {
  onChange: _propTypes2['default'].func,
  value: _propTypes2['default'].string,
  placeholder: _propTypes2['default'].string,
  onSelect: _propTypes2['default'].func,
  children: _propTypes2['default'].any
};
SelectInput.defaultProps = {
  onChange: function onChange() {}
};
exports['default'] = SelectInput;
module.exports = exports['default'];